﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace DFGK.Service
{
    public class UnitService : BaseService, IUnitService
    {
        private readonly IUnitRepository _unitRepository;
        private readonly IUnitOfWork _unitOfWork;
        public UnitService(IMapper mapper, IUnitRepository unitRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            _unitRepository = unitRepository;
            _unitOfWork = unitOfWork;
        }

        public bool AddUnitList(List<Unit> units, out string message)
        {
            message = string.Empty;
            foreach (var item in units)
            {
                item.Id = YitIdHelper.NextId();
                var employees = _unitRepository.QueryIsEntityWhere(x => x.UnitCode == item.UnitCode);
                if (employees != null)
                {
                    message = $"{item.UnitCode}编码已存在";
                    return false;
                }

            }
            _unitRepository.AddEntityList(units);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
